package projet.item;

import projet.Tools;

import projet.CustomException;
import projet.dataaccess.DataAccess;
import projet.dataaccess.PathErrorException;

/**
 * The CD class represents a CD object. Information about the CD is stored and
 * can be retrieved.
 * 
 * @author CORSINI Pierre, BRUN Sylvain
 * @version December 13, 2009
 */
/**
 * @author sbrun
 */
public class CD extends Item {
	private String artist;
	private Integer numberOfTracks;

	/**
	 * Create an empty CD.
	 * 
	 * @param id The future item's ID.
	 * @throws CustomException
	 */
	public CD(int id) throws CustomException {
		super();
		this.id = id;
		this.AskEdit();
	}

	/**
	 * @param id
	 * @param theTitle
	 * @param comment
	 * @param theArtist
	 * @param tracks
	 */
	public CD(int id, String theTitle, String comment, String theArtist,
			int tracks) {
		super(id, theTitle, comment, null);
		this.artist = theArtist;
		this.numberOfTracks = tracks;
	}

	/**
	 * @param dataAccess
	 * @throws PathErrorException
	 */
	public CD(DataAccess dataAccess) throws PathErrorException {
		super(dataAccess);
		this.artist = dataAccess.getElementString("artist");
		this.numberOfTracks = dataAccess.getElementInt("numberOfTracks");
	}

	/*
	 * (non-Javadoc)
	 * @see Item#AskEdit()
	 */
	@Override
	public void AskEdit() throws CustomException {
		super.AskEdit();

		String buffer;

		// Title
		System.out.print("- Artist: ");
		buffer = Tools.readLineEdit(this.artist);
		if (buffer.length() < 1)
			throw new CustomException(
					"Item artist name must at least have one character !");
		this.artist = buffer;

		// Time
		System.out.print("- Number of tracks: ");
		buffer = Tools.readLineEdit(this.numberOfTracks);
		try {
			this.numberOfTracks = Integer.parseInt(buffer);
		} catch (NumberFormatException e) {
			throw new CustomException("Number of tracksmust be a number !");
		}
	}

	/**
	 * @return The artist for this CD.
	 */
	public String getArtist() {
		return this.artist;
	}

	/**
	 * @return The number of tracks on this CD.
	 */
	public int getNumberOfTracks() {
		return this.numberOfTracks;
	}

	/*
	 * (non-Javadoc)
	 * @see projet.item.Item#play()
	 */
	@Override
	public void play() {
		System.out.println("Ce media ne se lit pas");
	}

	public DataAccess toMediaAccess() throws PathErrorException {
		DataAccess dataAccess = super.toMediaAccess();
		dataAccess.addElement("artist", this.artist);
		dataAccess.addElement("numberOfTracks", this.numberOfTracks.toString());

		return dataAccess;
	}

	/**
	 * Méthode affichage pour un show
	 */
	public void affichage() {
		super.affichage();
		Tools.print(2, "Artist      : " + this.artist);
		Tools.print(2, "Tracks      : " + this.numberOfTracks);
	}

	/*
	 * (non-Javadoc)
	 * @see Item#toString()
	 */
	@Override
	public String toString() {
		return super.toString() + "Artist:\"" + this.artist + "\" Tracks:\""
				+ this.numberOfTracks + "\"";
	}
}
